package com.jxb.jingchen.print.printer;

import android.app.Application;
import android.bluetooth.BluetoothDevice;
import android.graphics.Bitmap;
import android.graphics.Typeface;

import com.gengcon.www.jcprintersdk.JCAPI;
import com.gengcon.www.jcprintersdk.callback.Callback;
import com.gengcon.www.jcprintersdk.callback.PrintCallback;

/**
 * @author jinxb
 * date     : 2021/8/20
 * desc     :B3S\B11型号的打印机
 */
public class PrinterB3SB11 extends JCPrinter {
    private JCAPI mJCAPI;
    private PrintCallback mPrintCallback;

    public PrinterB3SB11(OnPrintListener onPrintListener) {
        super(onPrintListener);
        Callback callback = initCallback();
        this.mJCAPI = JCAPI.getInstance(callback);
        //初始化打印回调
        initPrintCallback();
    }

    private Callback initCallback() {
        /*
         * 创建全局回调
         */
        return new Callback() {
            @Override
            public void onConnectSuccess(String s, int i) {
                //打印机连接成功
                onPrintListener.onConnectSuccess();
            }

            @Override
            public void onDisConnect() {
                //打印机断开及连接失败回调 （SDK内部无广播。关闭打印机及关闭蓝牙此处不会回调。需要实时监听状态 需要用到蓝牙广播）
                onPrintListener.onDisConnect();
            }

            @Override
            public void onElectricityChange(int i) {
                //打印机电量变化
            }

            @Override
            public void onCoverStatus(int i) {
                //打印机上盖状态监听
            }

            @Override
            public void onPaperStatus(int i) {
                //打印机标签装入状态监听
            }

            @Override
            public void onPrinterIsFree(int i) {
                //打印机忙碌空闲状态监听
            }

            @Override
            public void onHeartDisConnect() {
                //心跳断开，此处应调用断开打印机
            }
        };
    }

    private void initPrintCallback() {
        mPrintCallback = new PrintCallback() {
            @Override
            public void onPrintProgress(final int i) {
            }

            @Override
            public void onPrintPageCompleted() {
                onPrintListener.onPrintSuccess();
                endPrint();
            }

            @Override
            public void onRibbonUsed(double v) {
                //碳带使用长度上报
            }

            @Override
            public void onPageNumberReceivingTimeout() {
                onPrintListener.onPrintFailed();
                endPrint();
            }

            @Override
            public void onAbnormalResponse(int i) {
                onPrintListener.onPrintFailed();
                endPrint();
            }
        };
    }
    private void endPrint() {
        mJCAPI.endJob();
    }

    @Override
    public void quitPrint() {
        mJCAPI.close();
    }

    @Override
    public void setPrintDensity(int density) {
        mJCAPI.setPrinterDensity(density);
    }

    @Override
    public void startJob(double pagerW, double pagerH, int orientation) {
        mJCAPI.startJob(pagerW, pagerH, orientation);
        mJCAPI.startPage();
    }

    @Override
    public void drawRectangle(double x, double y, double width, double height, double lineWidth) {
        mJCAPI.drawRectangle(x, y, width, height, lineWidth, 0, 0);
    }

    @Override
    public void draw2DQRCode(String qrCode, double codeX, double codeY, double qrCodeWidth) {
        mJCAPI.drawQrCode(qrCode, codeX, codeY, qrCodeWidth, 0);
    }

    //0x01  加粗
    //0x00  正常
    @Override
    public void drawText(String text, double x, double y, double width, double height, double fontSize, int align) {
        mJCAPI.drawText(text, x, y, width, height, fontSize, 0, 1.0f, (byte) 0x00, align, 0, true, Typeface.DEFAULT);
    }

    @Override
    public void draw1DBarcode(String text, double x, double y, double width, double height, double textHeight,
                              int position) {
        mJCAPI.drawBarCode(text, 20, x, y, width, height, textHeight, textHeight, position, 0);
    }

    @Override
    public void drawLine(double x1, double y1, double x2, double y2, double lineWidth) {
        mJCAPI.drawLine(x1, y1, x2, y2, lineWidth, 0);
    }

    @Override
    public void drawLineHorizontal(double x1, double y1, double length, double lineWidth) {
        mJCAPI.drawLine(x1, y1, x1 + length, y1, lineWidth, 0);
    }

    @Override
    public void drawLineVertical(double x1, double y1, double length, double lineWidth) {
        mJCAPI.drawLine(x1, y1, x1, y1 + length, lineWidth, 0);
    }

    @Override
    public void drawBitmap(Bitmap bitmap, double x, double y, double width, double height, int rotate) {
        mJCAPI.drawBitmap(bitmap, x, y, width, height, rotate);
    }

    @Override
    public void print(int quantity, int labelType) {
        mJCAPI.endPage();
        mJCAPI.commitJob(quantity, labelType, 4, mPrintCallback);
    }

    @Override
    public boolean connection(Application application, BluetoothDevice device) {
        int isOpenSuccess = mJCAPI.openPrinterByAddress(application, device.getAddress(), 0);
        return isOpenSuccess == 0;
    }
}
